package com.szgr.baoyin.volley;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.utils.RLog;

import java.util.Map;

/**
 * Created by yang on 15/12/30.
 */
public class Volley {
    public static void post(String url, final Map<String,String> map,final String tag,final VolleyInterface listener){
        StringRequest sr = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s(tag + ":" + s);
                        listener.onSuccess(s,tag);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                RLog.s(tag + " error:" + volleyError.toString());
                listener.onFailed(tag);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    public static void get(String url,final String tag,final VolleyInterface listener){
        StringRequest sr = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s(tag + ":" + s);
                        listener.onSuccess(s,tag);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                RLog.s(tag + " error:" + volleyError.toString());
                listener.onFailed(tag);
            }
        });
        MyApplication.getmInstance().addToRequestQueue(sr);
    }
}
